package com.morningstar.system.api.feign;

import cn.hutool.core.lang.UUID;
import com.morningstar.system.api.entity.SysUserFeign;
import com.morningstar.system.api.feign.fallback.SystemUserFeignFactory;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

import static com.morningstar.common.constants.feign.FeignClientConstants.SERVICE_SYSTEM;
import static com.morningstar.common.constants.feign.FeignUrlConstants.*;

/**
 * 系统用户远程调用
 * @author yls
 * @date 2022-02-10
 */
@FeignClient(value = SERVICE_SYSTEM, configuration = ISystemUserFeign.Config.class, fallbackFactory = SystemUserFeignFactory.class)
public interface ISystemUserFeign {

    /**
     * 根据用户Id获取用户信息
     *
     * @param userId 用户Id
     * @return 用户基本信息
     * @throws Exception 异常信息
     */
    @GetMapping(SYSTEM_USER_ID)
    SysUserFeign getUserById(@RequestParam("userId") String userId) throws Exception;

    /**
     * 根据用户Id获取用户信息
     *
     * @param userName          用户登录名
     * @return 用户基本信息
     * @throws Exception        异常信息
     */
    @GetMapping(SYSTEM_USER_NAME)
    SysUserFeign getUserByName(@RequestParam("userName") String loginName) throws Exception;

    /**
     * 根据用户手机号获取用户信息
     *
     * @param mobile            用户手机号
     * @return 用户基本信息
     * @throws Exception        异常信息
     */
    @GetMapping(SYSTEM_USER_MOBILE)
    SysUserFeign getUserByMobile(@RequestParam("mobile") String mobile) throws Exception;

    @GetMapping(SYSTEM_USER_PERMISSION)
    List<String> getPermissionList(@RequestParam("loginId") String loginId, @RequestParam("loginType") String loginType);

    @GetMapping(SYSTEM_USER_ROLE)
    List<String> getRoleList(@RequestParam("loginId") String loginId, @RequestParam("loginType") String loginType);

    /**
     * 根据用户获取用
     *
     * @param ids 用户Ids
     * @return 用户Id
     */
    @PostMapping(SELECT_USER_BY_IDS)
    List<Long> selectUserByIds(@RequestBody List<Long> ids);

    /**
     * 根据用户角色获取用户Ids
     *
     * @param ids 用户Ids
     * @return 用户Id
     */
    @PostMapping(SELECT_USER_BY_ROLE_IDS)
    List<Long> selectUserByRoleIds(@RequestBody List<Long> ids);

    /**
     * 根据用户部门获取用户Ids
     *
     * @param ids 用户Ids
     * @return 用户Id
     */
    @PostMapping(SELECT_USER_BY_DEPT_IDS)
    List<Long> selectUserByDeptIds(@RequestBody List<Long> ids);

    /**
     * 根据职位获取用户ids
     *
     * @param ids 用户Ids
     * @return 用户Id
     */
    @PostMapping(SELECT_USER_BY_POST_IDS)
    List<Long> selectUserByPostIds(@RequestBody List<Long> ids);

    /**
     * 查询部门领导
     *
     * @param ids 部门Ids
     * @return 部门领导Id
     */
    @PostMapping(SELECT_USER_BY_DEPT_LEADER_IDS)
    List<Long> selectUserByDeptLeaderIds(@RequestBody List<Long> ids);

    /**
     * 获取用户Id和用户名
     *
     * @return 基本结果类
     */
    @GetMapping(SYSTEM_USER_ID_AND_NAME)
    Map<Long, String> selectUserIdAndName();

    /**
     * 用户登录成功
     *
     * @param loginId 用户登录Id
     * @param ip      Ip
     */
    @GetMapping(SYSTEM_USER_LOGIN_SUCCESS)
    void userLoginSuccess(@RequestParam("loginId") Long loginId,
                          @RequestParam("loginIp") String ip);

    class Config {

//        @Value("${feign.token}")
//        String feignToken;

        @Value("${spring.application.name}")
        String applicationName;

        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }

        @Bean
        RequestInterceptor interceptor() {
            return template -> {
                template.header("X-Request-Id", UUID.randomUUID().toString());
//                template.header("X-Signature", feignToken);
                template.header("X-Application-Name", applicationName);
            };
        }
    }
}
